package com.kefu.boss.vo;

import javax.validation.constraints.NotEmpty;

import cn.hutool.core.bean.BeanUtil;
import com.kefu.boss.model.BossUserRole;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import com.kefu.boss.model.BossUser;
import com.kefu.common.context.KefuContext;
import com.kefu.common.vo.AbstractInsertVo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@ApiModel(description = "")
public class BossUserInsertVo extends AbstractInsertVo<BossUser> {
    /**
     * 名称
     */
    @ApiModelProperty(value = "名称")
    private String userName;

    /**
     * 邮箱
     */
    @NotEmpty
    @ApiModelProperty(value = "邮箱")
    private String userEmail;

    /**
     * 创建人
     */
    @ApiModelProperty(name = "createUser", value = "创建人")
    private String createUser;

    /**
     * 密码
     */
    @NotEmpty
    @ApiModelProperty(name = "userPwd", value = "密码")
    private String userPwd;

    @ApiModelProperty(name = "roleIds", value = "角色id")
    private String[] roleIds;

    @Override
    public BossUser convert() {
        BossUser info = BeanUtil.toBean(this, BossUser.class);
        KefuContext context = KefuContext.getKefuContext();
	    info.setUserPwd(new BCryptPasswordEncoder().encode(userPwd));
        info.setCreateUser(context.getUserId());
	    info.setCreateTime(System.currentTimeMillis());
        info.setUserStatus(1);
	    return info;
	}

    public List<BossUserRole> roleConvert(String userId) {
        List<BossUserRole> list = new ArrayList<>();
        for (String roleId : roleIds) {
            BossUserRole bossUserRole = new BossUserRole();
            bossUserRole.setRoleId(roleId);
            bossUserRole.setUserId(userId);
            list.add(bossUserRole);
        }
        return list;
    }
}